package com.grapevine.contact.adapter;

import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;
import com.grapevine.contact.R;
import com.grapevine.contact.local.NumberLocalProvider;
import com.grapevine.contact.util.AndroidPhoneLocationLoader;

/**
 * 读取数据后，将数据库填充到指定好的行中
 * 
 * @author Snake
 * 
 */

public class RecentAdapter extends ResourceCursorAdapter
{
	private AndroidPhoneLocationLoader mAndroidPhoneLocationLoader;

	public RecentAdapter(Context context, int layout, Cursor c, AndroidPhoneLocationLoader loder)
	{
		super(context, layout, c);
		mAndroidPhoneLocationLoader = loder;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor)
	{
		// TODO Auto-generated method stub
		ImageView mailButton = (ImageView) view.findViewById(R.id.MailButton);
		TextView textNumber = (TextView) view.findViewById(R.id.TextNumber);
		TextView textDurai = (TextView) view.findViewById(R.id.TextDuration);
		TextView textName = (TextView) view.findViewById(R.id.TextName);

		int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
		long callTime = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
		String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
		String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));

		if (CallLog.Calls.INCOMING_TYPE == type)
		{
			mailButton.setBackgroundResource(R.drawable.ic_call_log_list_incoming_call); // 更改行颜色
		}
		else if (CallLog.Calls.OUTGOING_TYPE == type)
		{
			mailButton.setBackgroundResource(R.drawable.ic_call_log_list_outgoing_call);
		}
		else if (CallLog.Calls.MISSED_TYPE == type)
		{
			mailButton.setBackgroundResource(R.drawable.ic_call_log_list_missed_call);
		}

		CharSequence content = DateUtils.getRelativeTimeSpanString(callTime, System.currentTimeMillis(),
				DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE);
		textDurai.setText(content);
		
		if(TextUtils.isEmpty(name)){
			textName.setText(number);
			
			
			Cursor mLocation = context.getContentResolver().query(NumberLocalProvider.CONNECT_URI, null,
					NumberLocalProvider.NUMBER + "='" + number + "'", null, null);

			if (mLocation.moveToFirst())
			{
				String provice = mLocation.getString(mLocation.getColumnIndex(NumberLocalProvider.PROVINCE));
				String city = mLocation.getString(mLocation.getColumnIndex(NumberLocalProvider.CITY));
				String location = provice + "  " + city;
				textNumber.setText(location);
			}
			else
			{
				mAndroidPhoneLocationLoader.loadLocation(textNumber, number);
			}
			mLocation.close();
			
			
			
//			mAndroidPhoneLocationLoader.loadPhoto(textNumber, number);
		} else {
			textName.setText(name);
			textNumber.setText(number);
		}

	}
}